package eclipse.lib;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
import javax.imageio.*;
import eclipse.*;
import eclipse.util.*;
/**
 * An object representing a Form with a specified background.
 * The form's dimensions are determined by the background by default.
 */
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
public class StandardForm extends JFrame
{
	public static final long serialVersionUID = 1L;
	private background bImg;
	/**
	 * Redraws the form.
	 *
	 */
	public void redrawForm()
	{
		repaint();
	}
	/**
	 * Redraws the background to the form.
	 *
	 */
	public void redrawBackground()
	{
		bImg.repaint();
	}	
	/**
	 * Initializes and creates a form with specific properties.
	 * @param sTitle The title of the form as a String
	 * @param iXLoc The X-Location of the form as an integer
	 * @param iYLoc The Y-Location of the form as an integer
	 * @param uImg The path to the background as a URL.
	 */
	public StandardForm(String sTitle, int iXLoc, int iYLoc, URL uImg)
	{
		bImg = new background(uImg);
		Dimension dim = bImg.getSize();
		makeForm(sTitle, dim.width + 10, dim.height + 10, iXLoc, iYLoc);
		add(bImg);
	}
	/**
	 * Initializes and creates a form with specific properties
	 * @param sTitle The title of the form as a String.
	 * @param iXLoc The X-Location of the form as an integer
	 * @param iYLoc The Y-Location of the form as an integer
	 * @param sImg The path to the background image as a String
	 */
	public StandardForm(String sTitle, int iXLoc, int iYLoc, String sImg)
	{
		background bImg = new background(sImg);
		Dimension dim = bImg.getSize();
		makeForm(sTitle, dim.width + 10, dim.height + 10, iXLoc, iYLoc);
		add(bImg);
	}	
	
	/**
	 * Initializes and creates a form with specific properties.
	 * @param sTitle The title of the form as a String
	 * @param iXLoc The X-Location of the form as an integer
	 * @param iYLoc The Y-Location of the form as an integer
	 * @param eImg The background image as an ERL path.
	 */
	public StandardForm(String sTitle, int iXLoc, int iYLoc, ERL eImg)
	{
		background bImg = new background(eImg);
		Dimension dim = bImg.getSize();
		makeForm(sTitle, dim.width + 10, dim.height + 10, iXLoc, iYLoc);
		add(bImg);		
	}
	/**
	 * Initializes and creates a form with specific properties.
	 * @param sTitle The title of the form as a String
	 * @param iWidth The width of the form as an integer
	 * @param iHeight The height of the form as an integer
	 * @param iXLoc The X-Location of the form as an integer
	 * @param iYLoc The Y-Location of the form as an integer
	 */
	public StandardForm(String sTitle, int iWidth, int iHeight, int iXLoc, int iYLoc)
	{
		makeForm(sTitle, iWidth, iHeight, iXLoc, iYLoc);
	}
	/**
	 * Generates the form with specific properties.
	 * @param sTitle The title of the form as a String
	 * @param iWidth The width of the form as an integer
	 * @param iHeight The height of the form as an integer
	 * @param iXLoc The X-Location of the form as an integer
	 * @param iYLoc The Y-Location of the form as an integer
	 */
	public void makeForm(String sTitle, int iWidth, int iHeight, int iXLoc, int iYLoc)
	{		
		setSize(iWidth, iHeight);
		setLocation(iXLoc, iYLoc);
		addWindowListener
		(
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent ev)
				{					
					removeAll();
					System.gc();
					System.exit(0);
				}
			}			
		);
	}			
	/**
	 * A subclass that will have a background set to a specific image.
	 * This is used to set the background of the StandardForm Object	 	 
	 */
	private class background extends JPanel
	{
		private static final long serialVersionUID = 1L;
		private Image img;
		/**
		 * Initializes the background class with a specific Image.
		 * @param iImg
		 */
		public background(Image iImg)
		{
			setImage(iImg);
		}
		/**
		 * Initializes the background class with the path of an Image.
		 * @param sImg The path for an Image file represented with a String.
		 */
		public background(String sImg)
		{
			setImage(sImg);
		}
		/**
		 * Initializes the background class with the URL of an Image.
		 * @param uImg The path for an Image file represented with a URL.
		 */
		public background(URL uImg)
		{
			setImage(uImg);
		}
		/**
		 * Initializes the background class with the ERL of an Image.
		 * @param eImg The path for an Image file represented with an ERL.
		 */
		public background(ERL eImg)
		{
			setImage(eImg.openStream());
		}
		public void paintComponent(Graphics g) 
		{
		    g.drawImage(img, 0, 0, null);		 
		}
		/**
		 * Sets the background image to the Image specified.
		 * @param img An Image file that the JPanel's background will be set to.
		 */
		public void setImage(Image img)
		{
			this.img = img;
		    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
		    setPreferredSize(size);
		    setMinimumSize(size);
		    setMaximumSize(size);
		    setSize(size);
		    setLayout(null);		    		    	
		}
		/**
		 * Sets the background image to the Image specified.
		 * @param sImage A path to the Image file represented with a String.
		 */
		public void setImage(String sImage)
		{
			setImage(new ImageIcon(sImage).getImage());
		}
		/**
		 * Sets the background image to the Image specified.		 
		 * @param iImage An InputStream representing the Image file.
		 */
		public void setImage(InputStream iImage)
		{
			try
			{
				setImage(ImageIO.read(iImage));
			}
			catch(Exception e)
			{
				ErrorManager.handleError(e);
			}
		}
		/**
		 * Sets the background image to the Image specified.
		 * @param uImage A path to the Image file represented as a URL.
		 */
		public void setImage(URL uImage)
		{
			setImage(new ImageIcon(uImage).getImage());
		}
	}
}
